<?php

namespace Main\Api\Admin;

class admins extends adminController
{
	// 获取用户信息
    public function getuserinfo()
    {
		$head = $this->get_all_headers();
		if(isset($head['token'])){
			$res = $this->Model->getOnly(['sid' => $head['token']]);
			unset($res['password']);
			$this->success($res, 'ok');
	    }
    }
	
    // 更新数据, 带where
    public function update()
    {
		$datas = [];
        $email = \FC\post('email');
		$username = \FC\post('username');
        $datas['email'] = $email;
        $datas['username'] = $username;	
		$pass = \FC\post('pass');
		$pass2 = \FC\post('pass2');	
	    $email = $this->Model->checkInput('email', $email);
		if(empty($username)){
			$this->error(1, '用户名不能为空');
		}
		if(!$email){
			$this->error(1, '邮箱格式不正确');
		}		
		if(!empty($pass)){
			if($pass!=$pass2){
				$this->error(1, '两次密码不一样');
			}
			$pass = $this->Model->checkInput('password', $pass);
			if(!$pass){
				$this->error(1, '密码格式不正确');
			}
			$datas['password'] = $pass;
			// 添加这一行可以修改密码后强制更新
			$datas['sid'] =  md5($pass . $email . TIME);
		}
        $where['sid'] = $this->userToken;
        // 如果数据不存在，会返回空值，否则会返回被影响的行数。
        if ($this->Model->checkUpdate($datas,$where)) {
            $this->success('','更新成功');
        } else {
            $this->success('', '更新失败');
        }
    }
	
}
